package com.hxrfid.common.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

    public static byte[] createQrCodeZip(List<QrCodeWithCardNo> qrCodeList, String filenamePrefix) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try (ZipOutputStream zos = new ZipOutputStream(baos)) {
            for (int i = 0; i < qrCodeList.size(); i++) {
                QrCodeWithCardNo item = qrCodeList.get(i);
                String filename = filenamePrefix + "_" + (i + 1) + ".png";
                zos.putNextEntry(new ZipEntry(filename));
                zos.write(item.getImageBytes());
                zos.closeEntry();
            }
        }
        return baos.toByteArray();
    }

    public static class QrCodeWithCardNo {
        private String cardNo;
        private byte[] imageBytes;

        public QrCodeWithCardNo(String cardNo, byte[] imageBytes) {
            this.cardNo = cardNo;
            this.imageBytes = imageBytes;
        }

        public String getCardNo() {
            return cardNo;
        }

        public byte[] getImageBytes() {
            return imageBytes;
        }
    }
}
